package com.ztesoft.web.taskgenerator.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ztesoft.core.common.Page;
import com.ztesoft.framework.exception.BaseAppException;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.web.common.db.po.DmPO;
import com.ztesoft.web.filecommon.db.po.DmFPO;
import com.ztesoft.web.taskgenerator.db.po.DmTaskStagePO;
import com.ztesoft.web.taskgenerator.service.IDmTaskService;
import com.ztesoft.web.taskgenerator.service.IDmTaskStageService;

/**
 * <Description>dmtaskstage管理 <br>
 * 
 * @author codeCreater <br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @since V1.0<br>
 * @see com.ztesoft.web.taskgenerator.controller <br>
 */

@Controller
@RequestMapping("/taskgenerator/dmtaskstage")
public class DmTaskStageController {

    private static final ZTEsoftLogManager LOGGER = ZTEsoftLogManager
            .getLogger(DmTaskStageController.class);

    @Autowired
    private IDmTaskStageService dmTaskStageService;
    @Autowired
    private IDmTaskService dmTaskService;

    @RequestMapping("index")
    @ResponseBody
    public ModelAndView index(Model model) {
    	ModelAndView view = new ModelAndView("index");
    	view.addObject("moduleMainView","sysmonitoring/datarecovery/views/DataRecovery");
    	return view;
    }
    
    @RequestMapping("queryRecordByPage")
    @ResponseBody
    public Page<DmTaskStagePO> queryRecordByPage(DmTaskStagePO record,
    		Page<DmTaskStagePO> resultPage) throws BaseAppException {
    	resultPage = dmTaskStageService.selectByArgAndPage(record, resultPage);
    	return resultPage;
    }
    /**
     * 任务监控页面list
     * @param record
     * @param resultListPage
     * @return
     * @throws BaseAppException
     */
    @RequestMapping("queryListByPage")
    @ResponseBody
    public Page<DmTaskStagePO> queryListByPage(DmTaskStagePO record,
    		Page<DmTaskStagePO> resultListPage) throws BaseAppException {
    	resultListPage = dmTaskStageService.selectListByArg(record, resultListPage);
    	return resultListPage;
    }
    /**
     * 任务监控页面状态汇总
     * @param record
     * @param resultListPage
     * @return
     * @throws BaseAppException
     */
    @RequestMapping("queryListCount")
    @ResponseBody
    public Integer[] queryListCount(DmTaskStagePO record) throws BaseAppException {
    	Integer[] count = dmTaskStageService.countPauseTask(record);
    	return count;
    }
    /**
     *  明细展示
     * @param dmTaskId
     * @param state
     * @param resultListPage
     * @return
     * @throws BaseAppException
     */
    @RequestMapping("selectListInfo")
    @ResponseBody
    public Page<DmTaskStagePO> selectListInfo(Integer dmTaskId, String state,
    		Page<DmTaskStagePO> resultListPage) throws BaseAppException {
    	resultListPage.setLimit(Integer.MAX_VALUE);//不分页取值~
    	resultListPage = dmTaskStageService.selectListInfo(dmTaskId, state, resultListPage);
    	return resultListPage;
    }
    /**
     * 查询展示明细对话框上面的状态个数
     * @param dmTaskId
     * @return
     * @throws BaseAppException
     */
    @RequestMapping("selectStateCount")
    @ResponseBody
    public Integer[] selectStateCount(Integer dmTaskId) throws BaseAppException {
    	
    	Integer[] result  = dmTaskStageService.selectStateCount(dmTaskId);
    	//resultListPage = dmTaskStageService.selectListByArg(null, resultListPage);
    	return result;
    }
    
    @RequestMapping("startTask")
    @ResponseBody
    /**
     * 启动--系统
     * @param dmTaskId
     * @param state
     * @param resultListPage
     * @return
     * @throws BaseAppException
     */
    public List<String> startTask(String dmPlanIdStr) throws BaseAppException {
    	
    	List<String> result  = dmTaskStageService.startTask(dmPlanIdStr);
    	//resultListPage = dmTaskStageService.selectListByArg(null, resultListPage);
    	return result;
    }
    /**
     * 立即暂停
     * @param record
     * @param dmPlanId
     * @param resultListPage
     * @return
     * @throws BaseAppException
     */
    @RequestMapping("immediatelyPause")
    @ResponseBody
    public List<String> immediatelyPause(String dmPlanIdStr) throws BaseAppException {
    	
    	return dmTaskStageService.immediatelyPause(dmPlanIdStr);
    }
    /**
     * 延迟暂停
     * @param record
     * @param dmPlanId
     * @param resultListPage
     * @return
     * @throws BaseAppException
     */
    @RequestMapping("delayPause")
    @ResponseBody
    public List<String> delayPause(String dmPlanIdStr) throws BaseAppException {
    	
    	return dmTaskStageService.delayPause(dmPlanIdStr);
    }
    
    /**
     * 取消
     * @param record
     * @param dmPlanId
     * @param resultListPage
     * @return
     * @throws BaseAppException
     */
    @RequestMapping("cancelTask")
    @ResponseBody
    public List<String> cancelTask(String dmPlanIdStr) throws BaseAppException {
    	return dmTaskStageService.clickCaTask(dmPlanIdStr);
    }

    @RequestMapping("qryRecordInfo")
    @ResponseBody
    public DmTaskStagePO qryRecordInfo(@RequestParam(value = "dmInfoId",
            required = true) Integer dmInfoId) throws BaseAppException {
        DmTaskStagePO record = dmTaskStageService.selectByPrimaryKey(dmInfoId);
        return record;
    }
    
    @RequestMapping("qryLeftErrorInfo")
    @ResponseBody
    public List<DmPO> qryLeftErrorInfo(String dmInfoId, String state) throws BaseAppException {
        List<DmPO> record = dmTaskStageService.qryLeftErrorInfo(dmInfoId, state);
        return record;
    }
    
    @RequestMapping("qryRightErrorInfo")
    @ResponseBody
    public List<DmPO> qryRightErrorInfo(String dmInfoId, String state) throws BaseAppException {
        List<DmPO> record = dmTaskStageService.qryRightErrorInfo(dmInfoId, state);
        return record;
    }
    
    @RequestMapping("qryLeftErrorInfoFile")
    @ResponseBody
    public List<DmFPO> qryLeftErrorInfoFile(String dmInfoId, String state) throws BaseAppException {
        List<DmFPO> record = dmTaskStageService.qryLeftErrorInfoFile(dmInfoId, state);
        return record;
    }
    
    @RequestMapping("qryRightErrorInfoFile")
    @ResponseBody
    public List<DmFPO> qryRightErrorInfoFile(String dmInfoId, String state) throws BaseAppException {
        List<DmFPO> record = dmTaskStageService.qryRightErrorInfoFile(dmInfoId, state);
        return record;
    }
//    @RequestMapping("isDelayTask")
//    @ResponseBody
//    public boolean isDelayTask(String dmPlanIdStr) throws BaseAppException {
//    	 
//    	return dmTaskStageService.isDelayTask(dmPlanIdStr);
//    }
//    
}
